import DTO.LocaldataDTO;
import Entity.Localdata;
import DTO.UserDTO;
//weishenme不用加链接数据库代码xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import Method.Isexist;
import Method.Querydgutid;

/**
 * The sevelet class to insert Person into database
 */
@SuppressWarnings("Duplicates")
@WebServlet( urlPatterns = {"/Insertcord2"})
public class Insertcord2Servlet extends HttpServlet {

    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        LocaldataDTO localdataDTO;
        UserDTO userDTO = (UserDTO) request.getSession().getAttribute("userDTO");
        Querydgutid query=new Querydgutid();
        Localdata localdata= query.execute(userDTO.getUsername());
        if (localdata!=null) {
          localdataDTO=new LocaldataDTO(localdata.getUserid(), localdata.getPassword(), localdata.getLocalname(),localdata .getPhone(), userDTO.getUsername(), localdata.getImageurl());
          request.getSession().setAttribute("localdataDTO",localdataDTO);
        } else {
            EntityManager em = getEntityManager();
            EntityTransaction tx = em.getTransaction();
        try {
            String dgutid = userDTO.getUsername();
             localdata = new Localdata("no_userid", "123456", "no_name", "no_phone", userDTO.getUsername(), "picture/no.jpg");
            localdataDTO=new LocaldataDTO("no_userid", "123456", "no_name", "no_phone", userDTO.getUsername(), "picture/no.jpg");
            request.getSession().setAttribute("localdataDTO",localdataDTO);
            tx.begin();
            em.persist(localdata);
            tx.commit();
        } catch (Exception ex) {
        } finally {
            em.close();
        }
    }
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("通过莞工登录注册成功");
        response.sendRedirect("/HomePage.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>

}


